/**
 * 货架数据
 */


// type=1货架，2过道中间，3过道长，4牌子
function getList(){
    var formData = {};
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shelf/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//字符串
function getHtml(d){
    var h = '', i, ii, rowList, type;
    h += '<table>';
    for (i = 0; i < d.length; i ++) {
        h += '<tr>';
        rowList = d[i].rowList;
        for (ii = 0; ii < rowList.length; ii ++) {
            type = rowList[ii].type;
            if (type == 2) {
                h += '<td class="guodao">';
            } else if (type == 3) {
                h += '<td class="paizi">';
            } else {
                if (shelf_id == rowList[ii].id) {
                    h += '<td class="huojia hwed" title="货架信息">';
                } else {
                    h += '<td class="huojia" title="货架信息">';
                }
            }
            if (type > 1 || rowList[ii].title == '') {
                h += rowList[ii].type_name;
            }
            h += rowList[ii].title;
            // h += rowList[ii].ceng_str=='' ? '' : '-';
            // h += rowList[ii].ceng_str;
            if (type == 1) {
                h += '<div class="tbtn">';
                h += '<a href="/map/index.html?btn=1&shelf_id=' + rowList[ii].id + '&shelf_name=' + rowList[ii].title + '">改仓位</a>';
                h += '<a href="/map/index.html?btn=2&shelf_id=' + rowList[ii].id + '">改货物</a>';
                h += '<input type="hidden" id="shelf_name' + rowList[ii].id + '" value="' + rowList[ii].title + '"/>';
                h += '</div>';
            }
            h += '</td>';
        }
        h += '</tr>';
    }
    h += '</table>';
    $('#shelfList').html(h);
}


//货架添加弹窗
function popShelfAdd(){
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">添加一行货架</div>';
    h += '<div style="margin: 5px 0;">选择类型</div>';
    h += '<select id="type">';
    h += '<option value="1">货架</option>';
    h += '<option value="2">过道</option>';
    h += '</select>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="shelfAdd();">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//货架新增
function shelfAdd(){
    var formData = {
        type: $('#type').val(),
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shelf/add',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: res.msg});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//货架修改
function shelfModify(v){
    var title = $('#shelf_name').val();
    var formData = {
        id: v, title: title,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'shelf/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//货架编辑
function shelfOne(v){
    var hh = '';
    hh += '<style type="text/css">.header { margin-top: 10px; font-size: 14px; } .group { margin: 10px 0; padding: 5px; border: 1px solid #ddd; border-radius: 5px; } .title { font-size: 14px; color: #888; } .tobtn { padding: 0 5px; color: #0088CC; cursor: pointer; float: right; } .del { color: red; }</style>';
    hh += '<a href="javascript:;" onclick="popShelfAdd();">货架添加</a>';
    hh += '<div class="header">→仓位详情</div>';
    hh += '<div class="title">';
    hh += '货架编号：' + shelf_name;
    hh += '<span class="tobtn" onclick="popCengAdd(' + v + ');">加层</span>';
    hh += '<span class="tobtn" onclick="popShelfEdit(' + v + ')">修改</span>';
    hh += '<span id="cengList"></span>';
    hh += '</div>';
    $('#tlist').html(hh).show();
    var formData = {
        shelf_id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'ceng/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var h = '', i, child, ii;
            for (i = 0; i < d.length; i ++) {
                h += '<div class="group">';
                h += '<div class="title">';
                h += '层编号：' + d[i].title;
                h += '<input type="hidden" id="ceng' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '<span class="tobtn" onclick="popCwAdd(0,' + d[i].id + ', ' + v + ')">加仓位</span>';
                h += '<span class="tobtn del" onclick="cengDel(' + d[i].id + ')">删除</span>';
                h += '<span class="tobtn" onclick="popCengEdit(' + d[i].id + ');">修改</span>';
                h += '<input type="hidden" id="ceng_name' + d[i].id + '" value="' + d[i].title + '"/>';
                h += '</div>';
                child = d[i].childList;
                for (ii = 0; ii < child.length; ii ++) {
                    h += '<div class="group">';
                    h += '<div class="title">';
                    h += '仓位编号：' + child[ii].title;
                    h += '<span class="tobtn del" onclick="cwDel(' + child[ii].id + ');">删除</span>';
                    h += '<span class="tobtn" onclick="popCwEdit(' + child[ii].id + ', ' + d[i].id + ', ' + v + ')">修改</span>';
                    h += '<input type="hidden" id="cw_name' + child[ii].id + '" value="' + child[ii].title + '"/>';
                    h += '</div>';
                    h += '</div>';
                }
                h += '</div>';
            }
            $('#cengList').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//货架号编辑HTML
function popShelfEdit(v){
    var shelf_name = $('#shelf_name' + v).val();
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">修改货架</div>';
    h += '<div style="margin: 5px 0;">货架号</div>';
    h += '<input type="text" id="shelf_name" value="' + shelf_name + '"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="shelfModify(' + v + ');">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//修改层HTML
function popCengAdd(v){
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">修改层</div>';
    h += '<div style="margin: 5px 0;">层号</div>';
    h += '<input type="text" id="ceng_name" placeholder="输入层号"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="cengAdd(' + v + ');">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//层添加
function cengAdd(shelf_id){
    var title = $('#ceng_name').val();
    var formData = {
        title: title, shelf_id: shelf_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'ceng/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改层HTML
function popCengEdit(v){
    var ceng_name = $('#ceng_name' + v).val();
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">修改层</div>';
    h += '<div style="margin: 5px 0;">层号</div>';
    h += '<input type="text" id="ceng_name" value="' + ceng_name + '"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="cengModify(' + v + ');">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//层修改
function cengModify(v){
    var title = $('#ceng_name').val();
    var formData = {
        id: v, title: title,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'ceng/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//层删除
function cengDel(v){
    var formData = {
        id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'cangwei/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改仓位HTML
function popCwAdd(id, pid, shelf_id){
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">修改仓位</div>';
    h += '<div style="margin: 5px 0;">仓位号</div>';
    h += '<input type="text" id="cw_name" placeholder="输入仓位号"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="cwModify(' + id + ', ' + pid + ', ' + shelf_id + ');">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//修改仓位HTML
function popCwEdit(id, pid, shelf_id){
    var cw_name = $('#cw_name' + id).val();
    var h = '';
    h += '<style type="text/css">.popCnt { height: 200px; } .popCnt .title { margin-bottom: 30px; text-align: center; } </style>';
    h += '<div class="popMask" onclick="$(\'#formPop\').html(\'\');"></div>';
    h += '<div class="popCnt">';
    h += '<div class="title">修改仓位</div>';
    h += '<div style="margin: 5px 0;">仓位号</div>';
    h += '<input type="text" id="cw_name" placeholder="输入仓位号" value="' + cw_name+ '"/>';
    h += '<button class="close" onclick="$(\'#formPop\').html(\'\');">取消</button>';
    h += '<button onclick="cwModify(' + id + ', ' + pid + ');">保存</button>';
    h += '</div>';
    $('#formPop').html(h);
}


//仓位修改
function cwModify(id, pid){
    var title = $('#cw_name').val();
    var formData = {
        id: id, title: title, pid: pid,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'cangwei/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//仓位删除
function cwDel(id){
    var formData = {
        id: id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'cangwei/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}